package com.netease.xone.itemview;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

import protocol.meta.AppImage;
import protocol.meta.AppInfo;
import protocol.meta.AppInfoDetail;
import Utils.StackTraceUtil;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;

import com.netease.log.NTLog;
import com.netease.xone.R;
import com.netease.xone.widget.LoadingImageView;


public class RenderInfoImages implements IRenderInfoBase, OnClickListener{
    private static final String TAG = RenderInfoImages.class.getSimpleName();
    
    private View mRoot;
    private LoadingImageView mImage1;
    private LoadingImageView mImage2;
    private LoadingImageView mImage3;

    public RenderInfoImages(View v) {
        mRoot = v;
        mImage1 = (LoadingImageView)v.findViewById(R.id.image1);
        mImage2 = (LoadingImageView)v.findViewById(R.id.image2);
        mImage3 = (LoadingImageView)v.findViewById(R.id.image3);
        
        int width = v.getContext().getResources().getDimensionPixelSize(R.dimen.info_size_130);
        mImage1.setServerClipSize(width, 0);
        mImage2.setServerClipSize(width, 0);
        mImage3.setServerClipSize(width, 0);
        
        mImage1.setOnClickListener(this);
        mImage2.setOnClickListener(this);
        mImage3.setOnClickListener(this);
    }

    @Override
    public void renderView(Context context, AppInfo appInfo) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        
        List<AppImage> list = appInfo.getGallery();
        if (null != list && 0 < list.size()) {
            mRoot.setVisibility(View.VISIBLE);
        } else {
            mRoot.setVisibility(View.GONE);
            return;
        }
       
        String url = null;
        try {
            url = list.get(0).getUrl();
            url = encode(new URL(url));
            mImage1.setLoadingImage(url);
            url = list.get(1).getUrl();
            url = encode(new URL(url));
            mImage2.setLoadingImage(url);
            url = list.get(2).getUrl();
            url = encode(new URL(url));
            mImage3.setLoadingImage(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void renderView(Context context, AppInfoDetail appInfoDetail) {
        
    }
    
    @Override
    public void onClick(View v) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
    }
    
    public static String encode(URL url) {
        StringBuilder result = new StringBuilder();
        result.append(url.getProtocol());
        result.append(':');

        String authority = url.getAuthority();
        if (authority != null) {
            result.append("//");
            result.append(authority);
        }
        
        String path = url.getPath();
        int index = path.lastIndexOf("/") + 1;
        result.append(path.substring(0, index));
        String end = path.substring(index);
        if (end != null) {
            result.append(URLEncoder.encode(end));
        }
        
        return result.toString();
    }
}
